$(function () {
    // var loading = weui.loading('loading', {
    //     className: 'custom-classname'
    // });
    // 使用搜索条
    weui.searchBar("#searchBar");
    var s = $.trim(Common.fn.getUrlParam('s')),
        searchArr = [];
    // 存在输入的值
    if (s) {
        // 对JSON的title进行遍历
        for(i in Config.classify) {
            for(var j = 0;j < Config.classify[i].length;j++) {
                if(Config.classify[i][j].title.indexOf(s) >= 0) {
                    searchArr.push(Config.classify[i][j]);
                }
            }
        }
        for(var i = 0;i < searchArr.length;i++) {
            var item = '<div class="weui-flex__item search-item" data-id="' + searchArr[i].id + '"><div class="placeholder"><div class="s-mine-item"><img src="../img/classify/' + searchArr[i].id.toString().substr(0, 1) + '/' + searchArr[i].id + '.png"><p>' + searchArr[i].title + '</p></div></div></div>';
            $('.s-content').append(item);
        }
    }
    loading.hide();
});

// 关注按钮
$(document).on('click', '.attention-btn', function () {
    var state = $(this).attr('data-attention'),
        id = $(this).attr('data-id');
    if (state == 'true') {
        // 已关注
        weui.confirm(
            "真的要取消关注吗？",
            function () {
                // 取消关注
                result = Model.business.attendBusiness({
                    businessId: id
                });
                if (result.code) {
                    alert(result.message);
                    location.reload();
                    return false;
                }
                location.reload();
            },
            function () { }
        );
    } else {
        // 关注店铺
        var loading = weui.loading('loading', {
            className: 'custom-classname'
        });
        result = Model.business.attendBusiness({
            businessId: id
        });
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
        loading.hide();
        location.reload();
    }
});

// 店铺跳转
$(document).on('click', '.search-item', function () {
    location.href = 'list_goods.html?cid=' + $(this).attr('data-id');
});